<?php
/**
 * Created by PhpStorm.
 * User: XP
 * Date: 2017/10/30
 * Time: 8:55
 */
namespace app\index\controller;
use think\Db;
use \think\Session;
use \think\Request;
use think\Controller;
class Input extends Controller{
    public function material(){
        return $this->fetch();
    }

    //返回添加副编辑的表单
    public function materialSubeditor(){
        $this->assign('author_level',input('author_level'));
        $this->assign('book_id',input('book_id'));
        return $this->fetch();
    }

    //返回添加主编的表单
    public function materialEditor(){
        $this->assign('author_level',input('author_level'));
        $this->assign('book_id',input('book_id'));
        return view();
    }

    //返回管理员重置密码界面
    public function modify(){
        return view();
    }

    //返回添加专著表单界面
    public function monograph(){
        $user=Session::get('user');
        $this->assign('user',$user->name);
        return view();
    }

    //添加副编辑人员
    public function addEditor(){

        $result['code'] = 1;
        $result['msg'] = '';

        $book_id = input('book_id');
        $author['name'] = input('authorName');
        $author['workplace'] = input('authorObject');
        $author['level'] = input('author_level');

        //向作者表中插入作者信息
        $sql = ['author_name'=>$author['name'],'author_work_place'=>$author['workplace']];
        $data = Db::table('author')->insert($sql);
        $authorId = Db::table('author')->getLastInsID();

        //向教材与作者关系表中插入作者信息
        $sql = ['book_id'=>$book_id, 'author_id'=>$authorId, 'level'=>$author['level']];
        $data1 = Db::table('books_author')->insert($sql);

        if($data){
            if($data1){
                $this->success('插入成功');
            }
        }
        else{
            $this->error('插入失敗');
        }

    }

    //添加主编人员
    public function addMainEditor(){
        Db::transaction(function(){
            $author['authorName'] = input('authorName');
            $author['authorObject'] = input('authorObject');
            $author['authorAbout'] = input('authorAbout');
            $author['url'] = input('url');
            $sql = ['author_name'=>$author['authorName'],
                'author_work_place'=>$author['authorObject'],
                'author_introduction'=>$author['authorAbout'],
                'author_pic'=>$author['url']];

//            $isAdd = Db::table('author')
//                ->where('author_name','=',$author['authorName'])
//                ->where('author_work_place','=',$author['authorObject'])
//                ->where('author_introduction','=',$author['authorAbout'])
//                ->find();
//
//            if($isAdd){
//                $this->error('重复添加');
//            }

            $data = Db::table('author')->insert($sql);
            $authorId = Db::table('author')->getLastInsID();

            $sql = ['book_id'=>input('book_id'),
                'author_id'=>$authorId,
                'level'=>input('author_level')];

            $data1 = Db::table('books_author')->insert($sql);

            if($data){
                if($data1){
                    $this->success('插入成功');
                }
                else{
                    $this->error('插入失敗');
                }
            }
            else{
                $this->error('添加失敗');
            }
        });

    }

    //上传主编人员图片
    public function upload(){
//        $this->error($_FILES);
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('file');
        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){

            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                // 成功上传后 获取上传信息
                // 输出 jpg
                $res['ext']=$info->getExtension();
                // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
                $res['saveName']='/uploads/'. $info->getSaveName();
                // 输出 42a79759f284b767dfcb2a0197904287.jpg
                $res['fileName']= $info->getFilename();

                $this->success($res);
            }else{
                // 上传失败获取错误信息
                $info= $file->getError();
                $this->error($info);
            }
        }else{
            $this->error("file error");
        }
    }

    //删除当前教材下的主编或者副主编
    public function delAuthor(){

        $data['level'] = input('level');
        $data['book_id'] = input('book_id');

        $data1 = Db::table('books_author')
            ->where('book_id','=',$data['book_id'])
            ->where('level','=',$data['level'])
            ->find();

        $data['author_id'] = $data1['author_id'];
        $ret = Db::table('books_author')
            ->where('author_id','=',$data['author_id'])
            ->where('book_id','=',$data['book_id'])
            ->delete();

        $ret1 = Db::table('author')->where('author_id','=',$data['author_id'])->delete();


        if($ret&&$ret1){

            $this->success('删除成功');
        }
        else{
            $this->error('删除失败');
        }

    }

    //主编的编辑
    public function editEditor(){
        $data['level'] = input('level');
        $data['book_id'] = input('book_id');

        $data = Db::table('books_author')
            ->where('book_id','=',$data['book_id'])
            ->where('level','=',$data['level'])
            ->find();
        $author = Db::table('author')
            ->where('author_id','=',$data['author_id'])
            ->find();

        $this->assign('author_id',$author['author_id']);
        $this->assign('author_name',$author['author_name']);
        $this->assign('author_work_place',$author['author_work_place']);
        $this->assign('author_introduction',$author['author_introduction']);
        $this->assign('author_pic',$author['author_pic']);
        return view();
    }

    //副主编的编辑
    public function editSubEditor(){
        $data['level'] = input('level');
        $data['book_id'] = input('book_id');

        $data = Db::table('books_author')
            ->where('book_id','=',$data['book_id'])
            ->where('level','=',$data['level'])
            ->find();
        $author = Db::table('author')
            ->where('author_id','=',$data['author_id'])
            ->find();

        $this->assign('author_id',$author['author_id']);
        $this->assign('author_name',$author['author_name']);
        $this->assign('author_work_place',$author['author_work_place']);
        return view();
    }

    //副主编的编辑
    public function editOthers(){
        $author = Db::table('author')
            ->where('author_id','=',input('author_id'))
            ->find();

        $this->assign('author_id',$author['author_id']);
        $this->assign('author_name',$author['author_name']);
        $this->assign('author_work_place',$author['author_work_place']);
        return view();
    }

    //删除其他副主编或者编委的信息
    public function delOthers(){

        $data['book_id'] = input('book_id');
        $data['author_id'] = input('author_id');

        $ret = Db::table('books_author')
            ->where('book_id','=',$data['book_id'])
            ->where('author_id','=',$data['author_id'])
            ->delete();
        if($ret){
            $ret1 = Db::table('author')
                ->where('author_id','=',$data['author_id'])
                ->delete();
            if($ret1){
                $this->success('删除成功');
            }
            else{
                $this->error('删除作者信息失败');
            }
        }
        else{
            $this->error('删除作者与教材关系失败');
        }

}

    //插入其他或者编委人员
    public function addOthers(){

        $data['book_id'] = input('book_id');
        $data['name'] = input('authorName');
        $data['workplace'] = input('authorObject');
        $data['level'] = input('author_level');

        $sql['author_name'] = $data['name'];
        $sql['author_work_place'] = $data['workplace'];
        $res1 = Db::table('author')->insert($sql);
        if(!$res1){
            $this->success('插入作者信息失败');
        }
        $author_id = Db::table('author')->getLastInsID();
        $sql2['author_id'] = $author_id;
        $sql2['book_id'] = $data['book_id'];
        $sql2['level'] = $data['level'];
        $res = Db::table('books_author')->insert($sql2);

        if($res){
            $this->success('插入成功');
        }
        else{
            $this->error('插入失败');
        }

    }

}
